<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/31
 * Time: 9:18
 */

namespace App\Http\Controllers\Service;
use Illuminate\Support\Facades\DB;
use App\Models\V1\MemberModel;
use App\Models\Common\WxAppPayLogModel;

class WeixinBuyVip
{
    /**
     * 添加一个微信订单 | 未付款之前
     * @param $from string 来源 | 公众号
     * @param $goodsName sring 产品名称 | vip:会员VIP，
     * @param $goodsId  integer 产品ID | vip:1
     * @param $money float 订单金额(单位元)
     * @param $orderNumber string 订单编号
     * @param $unionId string 用户的unionid
     * @param $openId string 用户的openId
     * @param $name string 用户名称
     * @param $userId integer 用户ID
     * @param $prepayId string 预支付ID
     * */
    public static function add_wx_order($from,$goodsName,$goodsId,$money,$orderNumber,$unionId,$openId,$name='',$userId=0,$prepayId=""){

        $data   = [
            'pay_source'=> $from,
            'wx_name'   => $name,
            'openId'    => $openId,
            'unionid'   => $unionId,
            'trade_no'  => $orderNumber,
            'user_id'   => $userId,
            'goods_id'  => $goodsId,
            'goods_type'=> $goodsName,
            'pay_status'=> 0,
            'order_money'=> $money,
            'created_at' => date_time(),
            'prepay_id'  => $prepayId
        ];

        DB::table('wx_app_pay_log')->insert($data);
    }


    /**
     * 处理购买VIP的结果
     * @param $notify object
     * @param $from string 购买来源 | minibuy(小程序购买) | wxbuy(微信端购买)
     * @return boolean
     * */
    public static function hand_buy_vip_result($notify,$from){

        $tradeNo    = $notify->out_trade_no;    //订单号

        $orderInfo  = WxAppPayLogModel::where('trade_no',$tradeNo)->first();

        if($orderInfo->pay_status == 1){ //已经处理过支付结果

            return false;
        }

        $endTime    = (date('Y') + 1) . date('-m-d h:i:s');
        $now        = date_time();

        //检查该微信用户是否存在
        $memberModel    = new MemberModel();
        if($orderInfo->user_id > 0){

            $memberInfo     = $memberModel->get_member_info_by_id($orderInfo->user_id);

        }elseif(!empty($orderInfo->unionid)){

            $memberInfo     = $memberModel->get_member_info_by_unionid($orderInfo->unionid);
        }

        if($memberInfo)
        {
            $userId     = $memberInfo->UserID;
            //结束时间
            if(strtotime($memberInfo->personal_end_time) < time())
            {
                $endTime    = time();

            } else {

                $endTime    = strtotime($memberInfo->personal_end_time);
            }

            $year       = date('Y',$endTime) + 1;
            $endTime    = $year . "-" . date('m-d',$endTime);
            $data       = [
                'States'            => 1,
                'personal_end_time' => $endTime,
                'UserID'            => $userId,
                'ReleaseDate'       => date_time()
            ];
            $memberModel->update_member_info($data);
            $memberModel->change_member_endtime($userId);

        }else{

            //添加新会员
            $data = [
                "States"    =>1,
                "MemNumber" =>create_member_number(),
                "UserName"  =>"",
                "UserPassWord"=>"",
                "NickName"  =>$orderInfo->wx_name,
                "YourName"  =>$orderInfo->wx_name,
                "FilePath1" =>"",
                "LoginKey"  =>"",
                "WeiXID"    =>$orderInfo->openid,
                "VipNumber" =>"",
                "MemIntegral"=>"",
                "MemEndTime"=>$endTime,
                "personal_end_time"=>$endTime,
                "RegisterType"=>$from,
                "AppKey"    =>"",
                "DeleteMark"=>1,
                "ReleaseDate"=>$now,
                "AddDate"   =>$now,
                "wx_unionid"=>$orderInfo->unionid,
                "app_openid"=>"",
                "head_img"  =>"",
                "sex"       =>"",
                "city"      =>0,
                "country"   =>0,
                "email"     => ""
            ];
            $userId     = DB::table('memberinfo')->insertGetId($data);
        }

        //修改订单状态
        $orderInfo->user_id         = $userId;
        $orderInfo->pay_time        = $notify->time_end;
        $orderInfo->transaction_id  = $notify->transaction_id;
        $orderInfo->pay_money       = $notify->total_fee;
        $orderInfo->pay_status      = 1;
        $orderInfo->save();

        return $orderInfo;
    }

}